import 'package:flutter/material.dart';
import 'package:flutter_basic/basic/basic_view.dart';
import 'package:flutter_basic/modules/home/controller.dart';
import 'package:flutter_basic/modules/home/property_list/controller.dart';
import 'package:get/get.dart';
import 'controller.dart';

class PropertyHomePage extends BasicPage<PropertyHomePageController>{
  PropertyHomePage.bind(controller, {String? tag}) : super.bind(controller, tag: tag);

  @override
  Widget build(BuildContext context) {
    print('build-build:${this.runtimeType}');
    double height = ((Get.width - 10 * 3) / 4) * 2 + 10;
    return Scaffold(
      appBar: AppBar(
        title: Text('楼盘页面'),
        centerTitle: true,
      ),
      body: Container(
        color: Colors.blue,
        height: height,
        child: PageView.builder(
            itemCount: controller.pageNumber,
            itemBuilder: (context, index){
              return _buildGridView(index);
            }
        ),
      ),
    );
  }


  Widget _buildGridView(int pageIndex){
    return Container(
      color: Colors.blueGrey,
      child: GridView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _itemView(pageIndex, index);
        },
        physics: NeverScrollableScrollPhysics(),
        itemCount: pageIndex < controller.pageNumber - 1 ? controller.pageSize : controller.items.length - controller.pageSize * pageIndex,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: controller.column,
          mainAxisSpacing: 10.0,
          crossAxisSpacing: 10.0,
          childAspectRatio: 1,
        ),
      ),
    );
  }

  Widget _itemView(int pageIndex, int index){
    int indexOfList = pageIndex * controller.pageSize + index;
    String title = controller.items[indexOfList];
    return GestureDetector(
      onTap: (){
        PropertyListPageController proListPageController = Get.find<PropertyListPageController>(tag: viewTag);
        HomeController homeController = Get.find<HomeController>(tag: viewTag);
        proListPageController.info.value = title;
        homeController.pageController.nextPage(
            duration: Duration(milliseconds: 200),
            curve: Curves.ease,
        );
      },
      child: Container(
        color: Colors.red,
        margin: EdgeInsets.all(5),
        alignment: Alignment.center,
        child: Text(
          title,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
